package com.mioto.ld.module.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.mioto.ld.component.EditType;
import com.mioto.ld.module.product.model.ProductEventEditDTO;
import org.springframework.stereotype.Service;

import com.mioto.ld.module.product.dao.ProductEventDao;
import com.mioto.ld.module.product.model.ProductEvent;
import com.mioto.ld.module.product.service.IProductEventService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author mioto
 * @date 2024-05-14 21:05:42
 */
@Service("productEventService")
public class ProductEventServiceImpl implements IProductEventService{

    @Resource
    private ProductEventDao productEventDao;

    @Override
    public List<ProductEvent> findList(ProductEvent productEvent) {
        return productEventDao.findList(productEvent);
    }

    @Override
    public int insert(ProductEvent productEvent) {
        productEvent.initCreateData();
        return productEventDao.insertIgnoreNull(productEvent);
    }

    @Override
    public int update(ProductEvent productEvent) {
        return productEventDao.update(productEvent);
    }

    @Override
    public int updateIgnoreNull(ProductEvent productEvent) {
        return productEventDao.updateIgnoreNull(productEvent);
    }

    @Override
    public ProductEvent findByColumn(String column, Object value) {
        return productEventDao.findByColumn(column,value);
    }

    @Override
    public int deleteByColumn(String column, Object value) {
        return productEventDao.deleteByColumn(column,value);
    }

    @Override
    public int batchDelete(Integer[] ids) {
        return productEventDao.batchDelete(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(List<ProductEventEditDTO> productEventEdits) {
        int result = 0;
        if (CollUtil.isNotEmpty(productEventEdits)){
            result = 1;
            productEventEdits.forEach(p->{
                if (p.getEditType() == EditType.EDIT_TYPE_INSERT){
                    insert(p);
                }else if (p.getEditType() == EditType.EDIT_TYPE_UPDATE){
                    updateIgnoreNull(p);
                }else if (p.getEditType() == EditType.EDIT_TYPE_DELETE){
                    deleteByColumn("id",p.getId());
                }
            });
        }
        return result;
    }

    @Override
    public Map<Integer, String> findEventByDeviceIdAndIdentitys(Integer deviceId, Integer[] faultTypes) {
        List<ProductEvent> productEvents = productEventDao.findEventByDeviceIdAndIdentitys(deviceId,faultTypes);
        if (CollUtil.isNotEmpty(productEvents)){
            Map<Integer,String> tempMap = new HashMap<>(productEvents.size());
            for (ProductEvent productEvent : productEvents) {
                tempMap.put(productEvent.getId(),productEvent.getName());
            }
            return tempMap;
        }
        return null;
    }
}